<?php
namespace app\user\validate\user;

use think\Validate;

/**
 * 用户验证
 */
class Account extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'phone'       => 'require|mobile|checkUser',
        'password'    => 'require',
        'logintype'   => 'require|in:phone,user,verify', // phone:一键登录;user:账号登录;verify:验证码登录;
        'user'        => 'require',
        'verify'      => [
            'require',
            'regex' => "/^[0-9]{4,6}$/",
        ],
        'nick_name'   => [
            'require',
            'regex' => "/^[\S]{2,}$/",
        ],
        'avatar'      => 'require|integer',
        'gender'      => 'require|in:0,1,2',
        'token'       => 'require',
        'accessToken' => 'require',
        'type'        => 'require|in:weixin,qq,sina',
        'oauth'       => 'require',
        'code'        => 'require',
    ];

    protected $field = [
        'phone'     => '手机号',
        'password'  => '密码',
        'logintype' => '登录方式',
        'user'      => '账号',
        'verify'    => '验证码',
        'type'      => '登录方式',
        'oauth'     => '三方标识',
        'code'      => '设备识别码',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'phone.mobile'   => '手机号码格式不正确',
        'phone.unique'   => '手机号已注册',
        'logintype.in'   => '登录方式错误',
        'verify.regex'   => '验证码格式错误',
        'avatar.require' => '请确认头像',
        'avatar.regex'   => '头像参数错误',
        'gender.require' => '请确认性别',
        'gender.regex'   => '性别参数错误',
        'type.in'        => '登录方式错误',

    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'           => ['phone'],
        'reg'           => ['phone', 'password'],
        'edit'          => ['nick_name', 'avatar', 'gender'],
        'thirdlogin'    => ['type', 'oauth'],
        'ioslogin'      => ['code'],
        'ioslogin_bind' => ['phone', 'verify'],
        'logintype'     => ['logintype'], // 登录方式验证
        'loginuser'     => ['user', 'password'],        // 账号登录验证场景
        'loginphone'    => ['token', 'accessToken'],   // 手机一键登录验证场景
        // 'loginverify'   => ['phone', 'verify'],     // 验证码登录验证
        'pwdSet'        => ['password'], // 设置密码/重置密码
        'pwdReset'      => ['password'], // 重置密码
    ];

    public function sceneLoginverify()
    {
        return $this->only(['phone', 'verify'])->remove('phone', 'checkUser');
    }

    /**
     * 手机一键登录验证场景
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-05
     */
    // public function sceneLoginPhone()
    // {
    //     return $this->only(['phone'])
    //         ->remove('phone', 'unique');
    // }

    /**
     * 找回密码 - 手机验证 验证场景
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-06
     */
    public function scenePwdVerify()
    {
        return $this->only(['phone', 'verify'])
            ->remove('phone', 'checkUser');
    }

    /**
     * 找回密码 - 重置密码Sync
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-06
     */
    public function scenePwdResetSync()
    {
        return $this->only(['phone', 'password'])
            ->remove('phone', 'checkUser');
    }

    public function checkUser($value, $rule, $data)
    {
        $mhmId = $data['mhm_id'] ?? request()->mhm_id;
        $has   = \app\user\model\user\Account::where('phone', $value)->where('mhm_id', $mhmId)->value('id');

        if ($has) {
            return '手机号已注册';
        }

        return true;
    }

}
